<template>
  <a-spin :spinning='confirmLoading'>
    <j-form-container :disabled='formDisabled'>
      <a-form-model ref='form' :model='model' :rules='validatorRules' slot='detail'>
        <a-row>
          <a-col :span='12'>
            <a-form-model-item label='学校编码' :labelCol='labelCol' :wrapperCol='wrapperCol' prop='schoolCode'>
              <a-input v-model='model.schoolCode' placeholder='请输入学校编码'></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span='12'>
            <a-form-model-item label='学校简称' :labelCol='labelCol' :wrapperCol='wrapperCol' prop='schoolNickname'>
              <a-input v-model='model.schoolNickname' placeholder='请输入学校简称'></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span='12'>
            <a-form-model-item label='学校名称' :labelCol='labelCol' :wrapperCol='wrapperCol' prop='schoolName'>
              <a-input v-model='model.schoolName' placeholder='请输入学校名称'></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span='12'>
            <a-form-model-item label='学校位置' :labelCol='labelCol' :wrapperCol='wrapperCol' prop='schoolAddress'>
              <a-input v-model='model.schoolAddress' placeholder='请输入学校位置'></a-input>
            </a-form-model-item>
          </a-col>

          <a-col :span='12'>
            <a-form-model-item label='年级数量' :labelCol='labelCol' :wrapperCol='wrapperCol' prop='schoolGradeNumber'>
              <a-input v-model='model.schoolGradeNumber' placeholder='请输入年级数量'></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span='12'>
            <a-form-model-item label='班级数量' :labelCol='labelCol' :wrapperCol='wrapperCol' prop='schoolClassRoomNumber'>
              <a-input v-model='model.schoolClassRoomNumber' placeholder='请输入班级数量'></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span='12'>
            <a-form-model-item label='学生数量' :labelCol='labelCol' :wrapperCol='wrapperCol' prop='schoolStudentNumber'>
              <a-input v-model='model.schoolStudentNumber' placeholder='请输入学生数量'></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span='12'>
            <a-form-model-item label='老师数量' :labelCol='labelCol' :wrapperCol='wrapperCol' prop='schoolTeacherNumber'>
              <a-input v-model='model.schoolTeacherNumber' placeholder='请输入老师数量'></a-input>
            </a-form-model-item>
          </a-col>

          <a-col :span='12'>
            <a-form-model-item label='坐标经纬度' :labelCol='labelCol' :wrapperCol='wrapperCol' prop='schoolLonLat'>
              <a-input v-model='model.schoolLonLat' placeholder='请输入坐标经纬度'></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span='12'>
            <a-form-model-item label='学校类型' :labelCol='labelCol' :wrapperCol='wrapperCol' prop='schoolTtype'>
              <!--              <a-input-number v-model="model.schoolTtype" placeholder="请输入学校类型" style="width: 100%" />-->
              <j-dict-select-tag @change='handleChanegObjType' type='list' v-model='model.schoolTtype'
                                 dictCode='school_ttype' placeholder='请输入学校类型' />
            </a-form-model-item>
          </a-col>
          <a-col :span='12'>
            <a-form-model-item label='学校状态' :labelCol='labelCol' :wrapperCol='wrapperCol' prop='schoolStatus'>
              <!--              <a-input-number v-model="model.schoolStatus" placeholder="请输入学校状态" style="width: 100%" />-->
              <j-dict-select-tag @change='handleChanegObjType' type='list' v-model='model.schoolStatus'
                                 dictCode='school_status' placeholder='请输入学校状态' />
            </a-form-model-item>
          </a-col>
          <a-col :span='12'>
            <a-form-model-item label='学校链接' :labelCol='labelCol' :wrapperCol='wrapperCol' prop='schoolLink'>
              <a-input v-model='model.schoolLink' placeholder='请输入学校链接'></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span='24'>
            <a-form-model-item  :labelCol='labelCol' :wrapperCol='wrapperCol' label='图片上传' prop='schoolImg'>
              <j-image-upload style='' bizPath='ora/school' v-model='model.schoolImg'></j-image-upload>
            </a-form-model-item>
          </a-col>
          <a-col :span='24'>
            <a-form-model-item label='学校简介' :labelCol='labelCol' :wrapperCol='wrapperCol' prop='schoolIntroduce'>
              <a-textarea :rows='8' v-model='model.schoolIntroduce' placeholder='请输入学校简介'></a-textarea>
            </a-form-model-item>
          </a-col>
          <a-col :span='24'>
            <a-form-model-item label='学校备注' :labelCol='labelCol' :wrapperCol='wrapperCol' prop='schoolRemark'>
              <a-textarea :rows='4' v-model='model.schoolRemark' placeholder='请输入学校备注'></a-textarea>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

import { httpAction, getAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'

export default {
  name: 'OraSchoolForm',
  components: {},
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data() {
    return {
      model: {},
      labelCol: {
        xs: { span: 12 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 12 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      validatorRules: {
        schoolCode: [
          { required: true, message: '请输入学校编码!' }
        ],
        schoolStatus: [
          { required: true, message: '请输入学校状态（1：小学 2：中学）!' }
        ],
        schoolNickname: [
          { required: true, message: '请输入学校简称' }
        ],
        schoolAddress: [
          { required: true, message: '请输入学校位置' }
        ],
        schoolGradeNumber: [
          { required: true, message: '请输入年级数量' }
        ],
        schoolClassRoomNumber: [
          { required: true, message: '请输入班级数量' }
        ],
        schoolStudentNumber: [
          { required: true, message: '请输入学生数量' }
        ],
        schoolTeacherNumber: [
          { required: true, message: '请输入老师数量' }
        ],
        schoolTtype: [
          { required: true, message: '请输入学校类型' }
        ],
        schoolIntroduce: [
          { required: true, message: '请输入学校简介' }
        ],
        schoolImg: [
          { required: true, message: '请输入背景图片' }
        ],
        schoolName: [
          { required: true, message: '请输入学校名称' }
        ],
      },
      url: {
        add: '/school/oraSchool/add',
        edit: '/school/oraSchool/edit',
        queryById: '/school/oraSchool/queryById'
      }
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    }
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
  },
  methods: {
    add() {
      this.edit(this.modelDefault)
    },
    edit(record) {
      this.model = Object.assign({}, record)
      this.visible = true
    },
    submitForm() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          httpAction(httpurl, this.model, method).then((res) => {
            if (res.success) {
              that.$message.success(res.message)
              that.$emit('ok')
            } else {
              that.$message.warning(res.message)
            }
          }).finally(() => {
            that.confirmLoading = false
          })
        }

      })
    }
  }
}
</script>